<?php
	$param = pods_url_variable('p','get');
	$decrypted_slug = decrypt($param,'1b2u3z4z5r6');
	$newsletter_pod = new Pod('newsletter', $decrypted_slug);
	
	if(is_array($newsletter_pod->data)){
	
	$buzzr_cities = get_buzzr_cities();
	$city_id = $newsletter_pod->get_field('cities.id');
	
	$buzzr_categories = get_buzzr_categories('','t.is_local = 1 || t.is_daily = 1');
	$category_ids = $newsletter_pod->get_field('categories.id');
	if(!is_array($category_ids)){
		$category_ids = array($category_ids);	
	}
	
	$is_daily = $newsletter_pod->get_field('daily');
	$is_weekly = $newsletter_pod->get_field('weekly');
	
	//Security Nonce for Ajax Request
	$nonce = wp_create_nonce('newsletter_ajax_pref');
?>
<h1 class="edit_header"><img src="<?php echo BUZZR_THEME_URL.'/img/edit.png'?>" />Edit Preferences</h1>
<div id="email_pref" class="grid_12 prefix_1">

<form name="email_pref">

<label for="email_address" class="form_text">Email Address: </label><input type="text" name="email_address" id="email_address" value="<?php echo $newsletter_pod->data['name']; ?>" readonly /><br /><br />
<input type="hidden" name="pod_item_id" id="pod_item_id" value="<?php echo $newsletter_pod->data['id']; ?>" />

<label for="newsletter_city_pref" class="form_text">City: </label>
<select name="newsletter_city_pref" id="newsletter_city_pref">
<?php
foreach($buzzr_cities as $city_val){
      if($city_val['id'] == $city_id){
?>
      <option value="<?php echo $city_val['id']; ?>" selected="selected"><?php echo $city_val['name']; ?></option>
<?php }else{ ?>
      <option value="<?php echo $city_val['id']; ?>"><?php echo $city_val['name']; ?></option>
<?php }
} 
?>
</select><br /><br />

<div id="newsletter_categories">
<label for="categories" class="form_text">Preferred Categories: </label><br />
<?php foreach($buzzr_categories as $category_val){ 
	if(in_array($category_val['id'],$category_ids)){
?>
		<input checked="yes" type="checkbox" id="categories" name="categories" value="<?php echo $category_val['id']; ?>" class="form_category" /><?php echo $category_val['name']; ?><br />
<?php 
	}else{ 
?>
		<input type="checkbox" id="categories" name="categories" value="<?php echo $category_val['id']; ?>" class="form_category" /><?php echo $category_val['name']; ?><br />
<?php 
	}
} 
?>
</div>
<br />

<label for="newsletter_daily" class="form_text">Daily Newsletter</label>
<input type="checkbox" id="newsletter_daily" name="newsletter_daily" <?php if($is_daily == 1){ echo 'checked="yes"'; }else{ echo ''; } ?> /><br /><br />

<label for="newsletter_weekly" class="form_text">Weekly Newsletter</label>
<input type="checkbox" id="newsletter_weekly" name="newsletter_weekly" <?php if($is_weekly == 1){  echo 'checked="yes"'; }else{ echo ''; } ?> /><br /><br />

<input id="ajax_nonce_pref" name="ajax_nonce_pref" type="hidden" value="<?php echo $nonce; ?>" />
<input type="button" id="newsletter_submit" name="newsletter_submit" value="Save" />
<div id="loader_email_pref" class="loader"><img src="<?php echo BUZZR_WP_URL.'/wp-content/pfiles/images/loader.gif'; ?>" /></div>
<div id="newsletter_result"></div>
</form>

<script type="text/javascript">
jQuery(document).ready(function($){
	$('#loader').ajaxStart(function(){
	       $(this).fadeIn('slow');
    });
    
    $('#loader').ajaxStop(function(){
      $(this).fadeOut('slow');
    });

	var is_daily = <?php echo $is_daily; ?>;
	var is_weekly = <?php echo $is_weekly; ?>;
	
	$("#newsletter_submit").click(function(){
		$("#newsletter_result").empty();
		var email = $("#email_address").val();
		var pod_item_id = $("#pod_item_id").val();
		var newsletter_city_pref = $("#newsletter_city_pref").val();
		var ajax_nonce_pref = $("#ajax_nonce_pref").val();
		var allCats = [];
		$('#newsletter_categories :checked').each(function() {
			allCats.push($(this).val());
		});
		
		var ajax_nonce = $("#ajax_nonce").val();
		var newsletter_daily = $("#newsletter_daily").is(':checked');
		var newsletter_weekly = $("#newsletter_weekly").is(':checked');

		$.ajax({ 
          	type: "post", 
          	url: "<?php echo BUZZR_WP_URL.'/wp-content/plugins/buzzr_newsletter/edit_pref.php'; ?>",
          	data: "pod_item_id="+pod_item_id+"&newsletter_city_pref="+newsletter_city_pref+"&ajax_nonce_pref="+ajax_nonce_pref+'&newsletter_daily='+newsletter_daily+'&newsletter_weekly='+newsletter_weekly+'&allCats='+allCats,
          	success: function(msg){
          		$("#newsletter_result").append(msg);

          		if(newsletter_daily != is_daily && newsletter_daily == 0){
    				//alert('Unsubscribed');
    				_gaq.push(['_trackEvent', 'Daily Newsletter', 'UnSubscribed', email]);
    				is_daily = 0;
	    		}else if(newsletter_daily != is_daily && newsletter_daily == 1){
	    			//alert('Subscribed');
	    			_gaq.push(['_trackEvent', 'Daily Newsletter', 'Subscribed', email]);
    				is_daily = 1;
	    		}

          		if(newsletter_weekly != is_weekly && newsletter_weekly == 0){
    				//alert('Unsubscribed');
    				_gaq.push(['_trackEvent', 'Weekly Newsletter', 'UnSubscribed', email]);
    				is_weekly = 0;
	    		}else if(newsletter_weekly != is_weekly && newsletter_weekly == 1){
	    			//alert('Subscribed');
	    			_gaq.push(['_trackEvent', 'Weekly Newsletter', 'Subscribed', email]);
	    			is_weekly = 1;
	    		}
          	}
        });
	});
});
</script>
</div>

<?php }else{
	echo '<div id="email_pref" class="grid_12 prefix_1"><h2>Sorry there seems to be an error, please singup again</h2></div>';
}
?>